(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.profLimitFaciViewController', profLimitFaciViewController);
	profLimitFaciViewController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig' , 'authService', 'ajaxServiceFactory', 'validatorConfig', 'arithService'];
	function profLimitFaciViewController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, authService, ajaxServiceFactory, validatorConfig, arithService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("profLimitFaciViewController"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getProfLimitFaciOptions : {
				url : 'do/cda/dataList/profLimitFaciOptions',
				method : 'post'
			},
		});
		ctrl.dialogsModule = dialogsModule;
//		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.bigestSeqInCat = ctrl.dialogsModule.params.bigestSeqInCat;
		ctrl.profLimitFaci = {};
		ctrl.formName = "profLimitFaciForm";
		
		ctrl.init = function() {
			if(ctrl.validatorConfig && ctrl.validatorConfig.profLimitFacis) layoutConfig.formConfigs.profLimitFaciAttr.tableConfigs.table.validatorConfig = ctrl.validatorConfig.profLimitFacis.profLimitFaciAttrs;
			ctrl.profLimitFaciAttrInnerGridOptions = gridService.createInnerGridOptions($scope, layoutConfig.formConfigs.profLimitFaciAttr.tableConfigs.table, ctrlProfLimitFaciAttrRowSelectionChanged);
			ctrl.profLimitFaciAttrInnerGridOptions.data = 'vm.profLimitFaci.profLimitFaciAttrs';
			ctrl.profLimitFaciAttrInnerGridOptions.verifyData = 'vm.profLimitFaciOld.profLimitFaciAttrs';
			function ctrlProfLimitFaciAttrRowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedProfLimitFaciAttrs = selectedRows;
			}
			
			var profLimitFaciTmp = ctrl.dialogsModule.params.profLimitFaci || {};
			
			var params = {facilityCat : profLimitFaciTmp.facilityCat};
			initGeneral(params).then(function() {
				ctrl.layoutConfig = layoutConfig;
				ctrl.profLimitFaci = profLimitFaciTmp;
				ctrl.profLimitFaciOld = ctrl.dialogsModule.params.profLimitFaciOld;
				if (ctrl.profLimitFaciOld)
					ctrl.profLimitFaciOld.profLimitFaciAttrs = ctrl.profLimitFaciOld.profLimitFaciAttrs || [];
			});
		};
		
		function initGeneral (params) {
			var defer = $q.defer();
			getProfLimitFaciOptions(params).then(function(response) {
				ctrl.profFaciCatOptions = response.profFaciCatOptions;
				ctrl.facilityTypeOptions = response.facilityTypeOptions;
				ctrl.faciAttrSrcOptions = response.faciAttrSrcOptions;
				ctrl.limitPropOptions = response.limitPropOptions;
				defer.resolve();
			}, angular.noop);
			return defer.promise;
		};
		function getProfLimitFaciOptions(params) {
			var defer = $q.defer();
			ajaxService.getProfLimitFaciOptions(params).then(function(response) {
				var profLimitFaciOptions = {};
				if (response.data) {
					angular.forEach(response.data, function(value, key) {
						var tempValue = {};
						tempValue = value;
						profLimitFaciOptions[key] = tempValue;
					});
				}
				defer.resolve(profLimitFaciOptions);
			});
			return defer.promise;
		}
		
		ctrl.facilityTypeChanged = function(value, form, isFirstLoad) {
			if (ctrl.profLimitFaci.facilityType && !isFirstLoad) {
				ctrl.profLimitFaci.facilityCode = '';
				// clear data
				ctrl.profLimitFaci.profLimitFaciAttrs = [];
			}
		};
		
		ctrl.facilityCodeLookupCallback = function(result, option) {
			if (result) {
				ctrl.profLimitFaci.facilityCode = result[0].facilityCode;
				// clear data
				ctrl.profLimitFaci.profLimitFaciAttrs = [];
				// 
				if (result[0].facilityAttrList && result[0].facilityAttrList.length > 0) {
					if (!ctrl.profLimitFaci.profLimitFaciAttrs) {
						ctrl.profLimitFaci.profLimitFaciAttrs = [];
					}
					for (var i = 0; i < result[0].facilityAttrList.length; i++) {
						if (result[0].facilityAttrList[i].attrValueMode == 'Y') continue;
						ctrl.profLimitFaci.profLimitFaciAttrs.push({
							attrName : result[0].facilityAttrList[i].attrName
						});
					}
				}
			}
		};
		
		ctrl.propNameChanged = function(currRowObj, form, isFirstLoad) {
			if (currRowObj) {
				var propName = currRowObj.propName;
				if (propName) { 
					for (var i = 0; i < ctrl.limitPropOptions.length; i++) {
						var _limitProp = ctrl.limitPropOptions[i];
						if (_limitProp.value == propName) {
							currRowObj.propNameExtProperty = {};
							currRowObj.propNameExtProperty = _limitProp;
							// change need clear, but if init for eidt need retain
							if (!isFirstLoad) {
								currRowObj.dynamicParams = {};
							}
						}
					}
				}
			}
		};
		
		ctrl.propParmsMoreDataDyna = function (obj){
			var params = {};
			// for get dynaparm page
			params.parmCat = "PROD_LIMITPROP";
			params.parmSubcat = obj.attrName;
			// for data on edit page popup
			params.custDynaparm = {};
			params.custDynaparm.dynamicParams = obj.dynamicParams;
			modalService.openCustomDialogs({
			   'title': 'Cust More Data',
			   'dialogsModule' : 'cda.custdynaparm.popupview',
			   'params': params,
			   'btnCallback' : {
					'ok' : function(resultList) {
					}
				}	
			});
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
	}
})(angular);
